package avl.sv.shared;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.AuthProvider;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AVM_Properties {

    private static File getPropertiesFile() {
        try {
            File f = new File(System.getenv("APPDATA") + File.separator + "AVM");
            f.mkdirs();
            f = new File(f.getPath() + File.separator + "AVM.properties");
            if (!f.exists()) {
                f.createNewFile();
            }
            return f;
        } catch (IOException ex) {
            Logger.getLogger(AVM_Properties.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static Properties getProperties(){
        try {
            File f = getPropertiesFile();
            Properties p = new Properties();
            p.load(new FileInputStream(f));
            return p;
        } catch (IOException ex) {
            Logger.getLogger(AVM_Properties.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new Properties();
    }
    
    public static void setProperty(String key, String value){
        Properties p = new Properties();
        p.setProperty(key, value);
        setProperties(p);
    }
    
    public static void setProperties(Properties toSet){
        try {
            File f = getPropertiesFile();
            Properties oldProperties = getProperties();
            for (String key:toSet.stringPropertyNames()){
                oldProperties.setProperty(key, toSet.getProperty(key));
            }
            oldProperties.store(new FileOutputStream(f), "");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AVM_Properties.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AVM_Properties.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
